/**

 @Name：layuiAdmin 用户管理 管理员管理 角色管理
 @Author：star1029
 @Site：http://www.layui.com/admin/
 @License：LPPL
    
 */

layui.define(["table", "form", "util"], function (exports) {
  var $ = layui.$,
    table = layui.table,
    form = layui.form;

  function unid(source) {
    var target = [];
    for (var i = 0; i < source.length; i++) {
      target.push(source[i]["id"]);
      if (Array.isArray(source[i]["children"]) || source[i]["children"].length > 0) {
        target = target.concat(unid(source[i]["children"]));
      }
    }
    return target;
  }


  //角色管理
  table.render({
    elem: "#LAY-user-back-role",
    url: "/admin/role/page", //模拟接口
    contentType: "application/json",
    method: "post",
    cols: [
      [
        { type: "checkbox", fixed: "left" },
        { field: "id", width: 80, title: "ID", sort: true },
        { field: "name", title: "角色名", templet: function (data) { var str = ''; for (var i = 0; i < data.pid * 2; i++) { str += '--'; } str += data.name; return '<div>' + str + '</div>'; } },
        { field: "key", title: "角色标识" },
        { field: "pid", title: "上级角色" },
        {
          title: "操作",
          width: 150,
          align: "center",
          fixed: "right",
          toolbar: "#table-useradmin-admin"
        }
      ]
    ],
    page: true,
    limit: 30,
    height: "full-220"
  });

  //监听工具条
  table.on("tool(LAY-user-back-role)", function (obj) {
    var data = obj.data;
    if (obj.event === "del") {
      if (data.isSys) return;
      layer.confirm("确定删除此角色？", function (index) {
        $.post("/admin/role/delete", { id: data.id }, function (result) {
          if (result.data) {
            table.reload("LAY-user-back-role");
            layer.msg("已删除");
            layer.close(index);
          } else {
            layer.msg(result.msg);
          }
        });
      });
    } else if (obj.event === "edit") {
      var tr = $(obj.tr);
      if (data.isSys) return;
      layer.open({
        type: 2,
        title: "编辑角色",
        content: "/admin/role/edit?id=" + data.id,
        area: ["500px", "480px"],
        btn: ["确定", "取消"],
        yes: function (index, layero) {
          var iframeWindow = window["layui-layer-iframe" + index],
            submit = layero
              .find("iframe")
              .contents()
              .find("#LAY-user-role-submit");

          //监听提交
          iframeWindow.layui.form.on("submit(LAY-user-role-submit)", function (data) {
            var field = data.field; //获取提交的字段
            field["permissionsIds"] = unid(iframeWindow.urTree.getChecked());
            $.post("/admin/role/edit", field, function (result) {
              if (result.data) {
                table.reload("LAY-user-manage"); //数据刷新
                layer.close(index); //关闭弹层
              } else {
                layer.msg(result.msg);
              }
            });
          });
          submit.trigger("click");
        },
        success: function (layero, index) { }
      });
    }
  });

  exports("useradmin", {});
});
